package org.xukai.common.util;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.joda.time.Seconds;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 *Type : com.weiyi.ehr.utils
 *Author : TimLi
 *Desc :
 *Date : 2016-03-08 11:05
 */

public class DatetimeUtils {

    private final static Log LOG = LogFactory.getLog(DateTimeUtils.class);
    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String YMDHMSS = "yyyyMMddHHmmss";
    public static final String YMD = "yyyyMMdd";
    public static final String Y_M_D = "yyyy-MM-dd";
    public static final String HM = "HH:mm";
    public static final String HMS = "HH:mm:ss";

    public static final String Y_M_DTHHMMSS = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * 把日期字符串转成需要的格式返回
     *
     *param date         日期字符串
     *param sourceFormat 日期原来格式
     *param descFormat   需要转换的格式
     *return
     */
    public static String getStrDateFormat(String date, String sourceFormat, String descFormat) {
        String descDate = null;
        try {
            DateTimeFormatter soure = DateTimeFormat.forPattern(sourceFormat);
            return DateTime.parse(date, soure).toString(descFormat);
        } catch (Exception e) {
            LOG.error("格式转换失败,date:" + date + ",sourceformat:" + sourceFormat + ",descFormat:" + descFormat);
        }
        return descDate;
    }

    /**
     * 提前多少天几点执行订单操作
     *
     *param date     yyyyMMdd
     *param plusdays
     *param time     HH:mm:ss
     *return
     */
    public static boolean executeOrderIsNot(String date, int plusdays, String time) {
        try {
            DateTimeFormatter format = DateTimeFormat.forPattern(YMD);
            LocalDate thedate = LocalDate.parse(date, format).minusDays(plusdays);
            LocalTime temptime = new LocalTime(time);

            if (LocalDate.now().isBefore(thedate)) {
                return false;
            } else if (LocalDate.now().isAfter(thedate)) {
                return true;
            } else {
                if (LocalTime.now().isAfter(temptime)) {
                    return true;
                }
            }
        } catch (Exception e) {
            LOG.error("判断日期异常", e);
            return false;
        }
        return false;
    }

    /**
     * 判断某个日期是否为当前日期plusday天的日期
     *
     *param date
     *param plusday
     *return
     */
    public static boolean isEqualNowPlusDay(String date, int plusday) {
        try {
            DateTimeFormatter format = DateTimeFormat.forPattern(YMD);
            LocalDate thedate = LocalDate.parse(date, format);
            if (thedate.isEqual(LocalDate.now().plusDays(plusday))) {
                return true;
            }
        } catch (Exception e) {
            LOG.error("判断日期异常", e);
            return false;
        }
        return false;
    }

    /**
     * 判断某个日期是否为当前日期plusday天后的日期
     *
     *param date
     *param plusday
     *return
     */
    public static boolean isAfterNowPlusDay(String date, int plusday) {
        try {
            DateTimeFormatter format = DateTimeFormat.forPattern(YMD);
            LocalDate thedate = LocalDate.parse(date, format);
            if (thedate.isAfter(LocalDate.now().plusDays(plusday))) {
                return true;
            }
        } catch (Exception e) {
            LOG.error("判断日期异常", e);
            return false;
        }
        return false;
    }

    /**
     * 判断当前时间是否在某个时间点之前
     *
     *param time expmple：15:45
     *return
     */
    public static boolean isNowBeforeTime(String time) {
        try {
            LocalTime temptime = new LocalTime(time);
            if (LocalTime.now().isBefore(temptime)) {
                return true;
            }
        } catch (Exception e) {
            LOG.error("判断日期异常", e);
            return false;
        }
        return false;
    }

    /**
     * 把日期转成对应的字符串
     *
     *param thedate
     *param format
     *return
     */
    public static String dateCovertStr(Date thedate, String format) {
        try {
            String dateTime = new DateTime(thedate).toString(format);
            return dateTime;
        } catch (Exception e) {
            LOG.error("Date 格式化字符串失败", e);
            return null;
        }
    }

    /**
     * 比较两个相差的天数
     *
     *param strDate
     *param endDate
     *return
     */
    public static int intervalDays(Date strDate, Date endDate) {
        LocalDate strDateTime = new LocalDate(strDate);
        LocalDate enddateTime = new LocalDate(endDate);
        return Days.daysBetween(strDateTime, enddateTime).getDays();
    }

    /**
     * /** 判断字符串日期是否是当天
     *
     *param format
     *param strDate
     *return
     */
    public static boolean judgeStrToNow(String strDate, String format) {
        DateTimeFormatter formater = DateTimeFormat.forPattern(format);
        DateTime dateTime = DateTime.parse(strDate, formater);
        DateTime localTime = DateTime.parse(DateTime.now().toString(format), formater);
        if (dateTime.isEqual(localTime)) {
            return true;
        }
        return false;
    }

    /**
     * 比较两个日期字符串相差的天数
     *
     *param strdate
     *param endDate
     *param format
     *return
     */
    public static int compareDateStr(String strdate, String endDate, String format) {
        DateTimeFormatter formater = DateTimeFormat.forPattern(format);
        DateTime strtime = DateTime.parse(strdate, formater);
        DateTime endtime = DateTime.parse(endDate, formater);
        int days = Days.daysBetween(strtime, endtime).getDays();
        return days;
    }

    /**
     * 比较两个日期字符串相差的秒数
     *
     *param strdate
     *param endDate
     *param format
     *return
     */
    public static int intervalSeconds(Date strdate, Date endDate) {
        DateTime strDateTime = new DateTime(strdate);
        DateTime enddateTime = new DateTime(endDate);
        int seconds = Seconds.secondsBetween(strDateTime, enddateTime).getSeconds();
        return seconds;
    }

    /**
     * date日期增加天数返回指定格式的字符串
     *
     *param strDate
     *param days
     *param format
     *return
     */
    public static String dateplusDays(Date strDate, int days, String format) {
        DateTime localDate = new DateTime(strDate);
        String datestr = localDate.plusDays(days).toString(format);
        return datestr;
    }

    /**
     * 比较当前时间是否在时间内
     *
     *param strTime 开始时间 HH:mm:ss
     *param endTime 结束时间 HH:mm:ss
     *return
     */
    public static boolean BetweenTime(String strTime, String endTime) {
        LocalTime localTime = LocalTime.now();
        LocalTime strtimes = LocalTime.parse(strTime);
        LocalTime endtimes = LocalTime.parse(endTime);
        if (localTime.isAfter(endtimes) || localTime.isBefore(strtimes)) {
            return true;
        }
        return false;
    }

    public static Date getDateByFormat(String date, String format){
    	if (StringUtils.isEmpty(date)){
    		return new Date();
    	}
    	
    	if (StringUtils.isEmpty(format)){
    		format = YMD;
    	}
    	
    	DateTime strtime = null;
    	try {
    		DateTimeFormatter formater = DateTimeFormat.forPattern(format);
            strtime = DateTime.parse(date, formater);
		} catch (Exception e) {
			strtime = new DateTime();
		}
        return strtime.toDate();
    }
    @Test
    public void testDateTime(){
        DateTime dateTime1 = new DateTime();
        System.out.println(dateTime1); // out: 2016-02-26T16:02:57.582+08:00
        DateTime dateTime2 = new DateTime(2016,2,14,0,0,0);
        System.out.println(dateTime2); // out: 2016-02-14T00:00:00.000+08:00
        DateTime dateTime3 = new DateTime(1456473917004L);
        System.out.println(dateTime3); // out: 2016-02-26T16:05:17.004+08:00
        DateTime dateTime4 = new DateTime(new Date());
        System.out.println(dateTime4); // out: 2016-02-26T16:07:59.970+08:00
        DateTime dateTime5 = new DateTime("2016-02-15T00:00:00.000+08:00");
        System.out.println(dateTime5); // out: 2016-02-15T00:00:00.000+08:00

        DateTime dateTime2000Year = new DateTime(2000,2,29,0,0,0);
        System.out.println(dateTime2000Year); // out: 2000-02-29T00:00:00.000+08:00
        DateTime dateTime1997Year =
                dateTime2000Year.withYear(1997);
        System.out.println(dateTime1997Year); // out: 1997-02-28T00:00:00.000+08:00

        DateTime now = new DateTime();
        System.out.println(now); // out: 2016-02-26T16:27:58.818+08:00
        DateTime tomorrow = now.plusDays(1);
        System.out.println(tomorrow); // out: 2016-02-27T16:27:58.818+08:00
        DateTime lastMonth = now.minusMonths(1);
        System.out.println(lastMonth); // out: 2016-01-26T16:27:58.818+08:00
    }

    @Test
    public void test(){
        String dateStr1 = "2015-05-15 01:44:21";
        String dateStr2 = "2015-05-19 01:24:57";
        Date date1 = getDateByFormat(dateStr1, YMDHMS);
        Date date2 = getDateByFormat(dateStr2, YMDHMS);
        System.out.println(DatetimeUtils.intervalSeconds(date1, date2));

        while (DatetimeUtils.intervalSeconds(date1, date2) > 0) {


//            DateTime originDate = new DateTime(date1);
            Date startdate = DateUtils.addDays(date1, 3);
            if (DatetimeUtils.intervalSeconds(startdate, date2) > 0) {
                //caozuo date1 he startDate
            } else {
                //caozuo date1 he date2
            }
            date1 = startdate;
//            DateTime plusDate = originDate.plusDays(3);
            System.out.println(dateCovertStr(date1, YMDHMS));
//            date1 = plusDate.toDate();
        }
    }

}
